/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings;

import com.android.settings.R;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;
import static com.google.android.tv.settings.TvSettings.System.SCREEN_DIM_TIMEOUT;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.preference.PreferenceScreen;

import java.util.List;
import android.util.Log;
import com.hisense.settings.util.Wrapper;
/**
 * Handles settings menu for cc Text Custom settings.  
 *
 * @author huangjiufa
 */
public class CCTextCustomSettings extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener{
   
    private static final String TAG = "CCTextCustomSettings";

    private static final String KEY_TEXT_CHARACTER_SIZE = "cc_text_character_size";
    private static final String KEY_TEXT_CHARACTER_STYLE = "cc_text_character_style";
    private static final String KEY_TEXT_CHARACTER_COLOR = "cc_text_character_color";
    private static final String KEY_TEXT_CHARACTER_OPACITY = "cc_text_character_opacity";
    private static final String KEY_TEXT_EDGE_TYPE = "cc_text_edge_type";
    private static final String KEY_TEXT_EDGE_COLOR = "cc_text_edge_color";
    private static final String KEY_TEXT_BACKGROUND_OPACITY = "cc_text_background_opacity";
    private static final String KEY_TEXT_BACKGROUND_COLOR = "cc_text_background_color";
	
	//Small 0
	//Normal 1
	//Large 2
	private static final int VALUE_CHARACTER_SIZE = 0;

	//Style1 0
	//Style2 1
	//Style3 2
	//Style4 3
	//Style5 4
	//Style6 5
	//Style7 6
	// 
	private static final int VALUE_CHARACTER_STYLE = 0;

	//Black 0
	//White 1
	//Red 2
	//Green 3
	//Blue 4
	//Yellow 5
	//Magenta 6 
	//Cyan 7
	private static final int VALUE_CHARACTER_COLOR = 0;

	//Solid 0
	//Flashing 1
	//Translucent 2
	//Transparent 3
	private static final int VALUE_CHARACTER_OPACITY = 0;



	//Raised 0
	//Depressed 1
	//Uniform 2
	//Left Drop Shadow 3
	//Right Drop Shadow 4
	private static final int VALUE_EDGE_TYPE = 0;

	//Black 0
	//White 1
	//Red 2
	//Green 3
	//Blue 4
	//Yellow 5
	//Magenta 6 
	//Cyan 7
	private static final int VALUE_EDGE_COLOR = 0;


	//Solid 0
	//Flashing 1
	//Translucent 2
	//Transparent 3
	private static final int VALUE_BACKGROUND_OPACITY = 0;

	//Black 0
	//White 1
	//Red 2
	//Green 3
	//Blue 4
	//Yellow 5
	//Magenta 6 
	//Cyan 7
	private static final int VALUE_BACKGROUND_COLOR = 0;

    private ListPreference mCharacterSizePreference;
    private ListPreference mCharacterStylePreference;
    private ListPreference mCharacterColorPreference;
    private ListPreference mCharacterOpacityPreference;
    private ListPreference mEdgeTypePreference;
    private ListPreference mEdgeColorPreference;
    private ListPreference mBackgroundColorPreference;
    private ListPreference mBackgroundOpacityPreference;
	



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.cc_text_custom_settings);


        mCharacterSizePreference = (ListPreference) findPreference(KEY_TEXT_CHARACTER_SIZE);
        final int currentCharacterSize = getCharacterSize(VALUE_CHARACTER_SIZE);

        mCharacterSizePreference.setValue((String.valueOf(currentCharacterSize)));
        mCharacterSizePreference.setOnPreferenceChangeListener(this);
		updateCharacterSizeSummary(String.valueOf(currentCharacterSize));

        mCharacterStylePreference = (ListPreference) findPreference(KEY_TEXT_CHARACTER_STYLE);
        final int currentCharacterStyle = getCharacterStyle(VALUE_CHARACTER_STYLE);

        mCharacterStylePreference.setValue((String.valueOf(currentCharacterStyle)));
        mCharacterStylePreference.setOnPreferenceChangeListener(this);
		updateCharacterStyleSummary(String.valueOf(currentCharacterStyle));
		
        mCharacterColorPreference = (ListPreference) findPreference(KEY_TEXT_CHARACTER_COLOR);
        final int currentCharacterColor = getCharacterColor(VALUE_CHARACTER_COLOR);

        mCharacterColorPreference.setValue((String.valueOf(currentCharacterColor)));
        mCharacterColorPreference.setOnPreferenceChangeListener(this);
		updateCharacterColorSummary(String.valueOf(currentCharacterColor));
		

        mCharacterOpacityPreference = (ListPreference) findPreference(KEY_TEXT_CHARACTER_OPACITY);
        final int currentCharacterOpacity = getCharacterOpacity(VALUE_CHARACTER_OPACITY);

        mCharacterOpacityPreference.setValue((String.valueOf(currentCharacterOpacity)));
        mCharacterOpacityPreference.setOnPreferenceChangeListener(this);
		updateCharacterOpacitySummary(String.valueOf(currentCharacterOpacity));

		
        mEdgeTypePreference = (ListPreference) findPreference(KEY_TEXT_EDGE_TYPE);
        final int currentEdgeType = getEdgeType(VALUE_EDGE_TYPE);

        mEdgeTypePreference.setValue((String.valueOf(currentEdgeType)));
        mEdgeTypePreference.setOnPreferenceChangeListener(this);
		updateEdgeTypeSummary(String.valueOf(currentEdgeType));
		
        mEdgeColorPreference = (ListPreference) findPreference(KEY_TEXT_EDGE_COLOR);
        final int currentEdgeColor = getEdgeColor(VALUE_EDGE_COLOR);

        mEdgeColorPreference.setValue((String.valueOf(currentEdgeColor)));
        mEdgeColorPreference.setOnPreferenceChangeListener(this);
		updateEdgeColorSummary(String.valueOf(currentEdgeColor));

        mBackgroundColorPreference = (ListPreference) findPreference(KEY_TEXT_BACKGROUND_COLOR);
        final int currentBackgroundColor = getBackgroundColor(VALUE_BACKGROUND_COLOR);

        mBackgroundColorPreference.setValue((String.valueOf(currentBackgroundColor)));
        mBackgroundColorPreference.setOnPreferenceChangeListener(this);
		updateBackgroundColorSummary(String.valueOf(currentBackgroundColor));

		mBackgroundOpacityPreference = (ListPreference) findPreference(KEY_TEXT_BACKGROUND_OPACITY);
		final int currentBackgroundOpacity = getBackgroundOpacity(VALUE_BACKGROUND_OPACITY);
		
		mBackgroundOpacityPreference.setValue((String.valueOf(currentBackgroundOpacity)));
		mBackgroundOpacityPreference.setOnPreferenceChangeListener(this);
		updateBackgroundOpacitySummary(String.valueOf(currentBackgroundOpacity));
				
		
    }



    private int getCharacterSize(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getTextSize();
		Log.d(TAG, "--------getCharacterSize---value=: " + value);
		if (value > 0)
		{
			value = value - 1;
		}
        return value;
    }


    private void setCharacterSize(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
		Wrapper.getWrapperInstance().setTextSize(value +1);
        

    }


    private void updateCharacterSizeSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_character_size_entries);
        CharSequence[] values = mCharacterSizePreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mCharacterSizePreference.setSummary(summaries[i]);
                break;
            }
        }
    }



    private int getCharacterStyle(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getTextStyle();
		Log.d(TAG, "--------getCharacterStyle---value=: " + value);
		if (value > 0)
		{
			value = value - 1;
		}

        return value;
    }


    private void setCharacterStyle(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
		Wrapper.getWrapperInstance().setTextStyle(value+1 );
        

    }



    private void updateCharacterStyleSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_character_style_entries);
        CharSequence[] values = mCharacterStylePreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mCharacterStylePreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    private int getCharacterOpacity(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getTextOpacity();
				Log.d(TAG, "--------getCharacterOpacity---value=: " + value);
		if (value > 0)
		{
			value = value - 1;
		}
        return value;
    }


    private void setCharacterOpacity(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
		Wrapper.getWrapperInstance().setTextOpacity(value+1);

    }



    private void updateCharacterOpacitySummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_character_opacity_entries);
        CharSequence[] values = mCharacterOpacityPreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mCharacterOpacityPreference.setSummary(summaries[i]);
                break;
            }
        }
    }
	
    private int getCharacterColor(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getTextColor();
		Log.d(TAG, "--------getCharacterColor---value=: " + value);
		if (value > 0)
		{
			value = value - 1;
			//Log.d(TAG, "--------kkkkkkkkkkgetCharacterColor---color=: " + value);
		}
        return value;
    }


    private void setCharacterColor(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        	Log.d(TAG, "--------455555555555555555---setCharacterColor=: " + value);
		Wrapper.getWrapperInstance().setTextColor(value +1);

    }



    private void updateCharacterColorSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_character_color_entries);
        CharSequence[] values = mCharacterColorPreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mCharacterColorPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    private int getEdgeType(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
		value = Wrapper.getWrapperInstance().getTextEdgeType();
		Log.d(TAG, "--------getEdgeType---value=: " + value);
		if (value > 0)
		{
			value = value - 1;
		}

        return value;
    }


    private void setEdgeType(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
            	Log.d(TAG, "--------22222222222222333333333---setEdgeType=: " + value);
		Wrapper.getWrapperInstance().setTextEdgeType(value+1);

    }



    private void updateEdgeTypeSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_edge_type_entries);
        CharSequence[] values = mEdgeTypePreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mEdgeTypePreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    private int getEdgeColor(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getTextEdgeColor();
			Log.d(TAG, "--------getEdgeColor---value=: " + value);
		if (value > 0)
		{
		        //Log.d(TAG, "-------888876678484784666---getEdgeColor=: " + value);
			value = value - 1;
		}
        return value;
    }


    private void setEdgeColor(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
                    	Log.d(TAG, "-------777777777777---setEdgeColor=: " + value);
		Wrapper.getWrapperInstance().setTextEdgeColor(value +1);

    }



    private void updateEdgeColorSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_edge_color_entries);
        CharSequence[] values = mEdgeColorPreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mEdgeColorPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    private int getBackgroundOpacity(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getBackgroundOpacity();
			Log.d(TAG, "--------getBackgroundOpacity---value=: " + value);
		if (value > 0)
		{
		       // Log.d(TAG, "------getBackgroundOpacity: " + value);
			value = value - 1;
		}
        return value;
    }


    private void setBackgroundOpacity(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
                Log.d(TAG, "------setBackgroundOpacity: " + value);
		Wrapper.getWrapperInstance().setBackgroundOpacity(value+1);

    }



    private void updateBackgroundOpacitySummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_background_opacity_entries);
        CharSequence[] values = mBackgroundOpacityPreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mBackgroundOpacityPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    private int getBackgroundColor(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		value = Wrapper.getWrapperInstance().getBackgroundColor();
		Log.d(TAG, "--------getBackgroundColor---value=: " + value);
		if (value > 0)
		{
			value = value - 1;
		}
        return value;
    }


    private void setBackgroundColor(int defaultValue) {
        int value = defaultValue;
        // TODO huangjiufa 
        
		Wrapper.getWrapperInstance().setBackgroundColor(value +1);
    }



    private void updateBackgroundColorSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_text_background_color_entries);
        CharSequence[] values = mBackgroundColorPreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mBackgroundColorPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

		
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return false;
    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
        if (KEY_TEXT_CHARACTER_SIZE.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setCharacterSize(value);
				updateCharacterSizeSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_CHARACTER_STYLE.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setCharacterStyle(value);
				updateCharacterStyleSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_CHARACTER_OPACITY.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setCharacterOpacity(value);
				updateCharacterOpacitySummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_CHARACTER_COLOR.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setCharacterColor(value);
				updateCharacterColorSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_EDGE_TYPE.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setEdgeType(value);
				updateEdgeTypeSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_EDGE_COLOR.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setEdgeColor(value);
				updateEdgeColorSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_BACKGROUND_OPACITY.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setBackgroundOpacity(value);
				updateBackgroundOpacitySummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        else if (KEY_TEXT_BACKGROUND_COLOR.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setBackgroundColor(value);
				updateBackgroundColorSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }

        return true;
    }

}
